package lib.self.utils;

import android.content.Context;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.runtime.PermissionRequest;

import java.util.List;

import lib.self.AppEx;
import lib.self.LogMgr;
import lib.self.ex.interfaces.IPermissionListener;

/**
 * description:动态申请权限工具类
 * author: Steven Xiao
 * date 2018/9/4
 */
public class PermissionUtil {
    private static final String TAG = PermissionUtil.class.getSimpleName();

    /**
     * 申请权限--单个申请
     *
     * @param context
     * @param listener    没有特殊权限可传null
     * @param rationale   权限重新申请
     * @param permissions 权限名称
     */
    public static void requestPermission(Context context, IPermissionListener listener, Rationale rationale, String... permissions) {
        if (listener == null) {
            listener = getPermissionListener();
        }
        if (null == context){
            context = AppEx.getCt();
            rationale = null;
        }
        IPermissionListener finalListener = listener;
        PermissionRequest request = AndPermission.with(context)
                .runtime()
                .permission(permissions);
        if (null != rationale){
            request = request.rationale(rationale);
        }
        request.onGranted(data -> finalListener.onGranted(data))
                .onDenied(data -> finalListener.onDenied(data))
                .start();
    }

    public static void requestPermission(IPermissionListener listener, Rationale rationale, String... permissions) {
        requestPermission(null, listener, rationale, permissions);
    }

    /**
     * 申请权限--单个申请
     *
     * @param listener  没有特殊权限可传null
     * @param rationale 权限重新申请
     * @param groups    权限组
     */
    public static void requestPermission(Context context, IPermissionListener listener, Rationale rationale, String[]... groups) {
        if (listener == null) {
            listener = getPermissionListener();
        }
        if (null == context){
            context = AppEx.getCt();
            rationale = null;
        }
        IPermissionListener finalListener = listener;
        PermissionRequest request = AndPermission.with(context)
                .runtime()
                .permission(groups);
        if (null != rationale){
            request = request.rationale(rationale);
        }
        request.onGranted(data -> finalListener.onGranted(data))
                .onDenied(data -> finalListener.onDenied(data))
                .start();
    }

    private static IPermissionListener getPermissionListener() {
        IPermissionListener listener = new IPermissionListener() {
            @Override
            public void onGranted(List<String> list) {
                for (int i = 0; i < list.size(); i++) {
                    LogMgr.d(TAG, "onGranted permission = " + list.get(i));
                }
            }

            @Override
            public void onDenied(List<String> list) {
                for (int i = 0; i < list.size(); i++) {
                    LogMgr.e(TAG, "onDenied permission = " + list.get(i));
                }
            }
        };
        return listener;
    }
}
